package com.example.qixinshangwutong.model.home;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.view.activity.NewsActivity;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class HomeNewsAdapter extends RecyclerView.Adapter <HomeNewsAdapter.myHolder> {
    private Context context;
    private List<HomeNewsModel> newsData;

    public void setNewsData(List<HomeNewsModel> newsData) {
        this.newsData = newsData;
    }

    public HomeNewsAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public myHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        return new myHolder(LayoutInflater.from(context).inflate(R.layout.home_news_item,null));
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull myHolder holder, int position) {
        if (!newsData.get(position).getTime().isEmpty()){
            Glide.with(context).load(newsData.get(position).getUrl()).into(holder.newsImage);
        }else {
            holder.newsImage.setImageResource(R.drawable.shouye);
        }
        holder.newTittle.setText(newsData.get(position).getTitle());
        holder.newsTime.setText(newsData.get(position).getTime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, NewsActivity.class);
                intent.putExtra("name",newsData.get(position).getTitle());
                intent.putExtra("uri","http://android.qxswt.com/mediadetail/"+newsData.get(position).getId());
                context.startActivity(intent);
            }
        });


    }


    @Override
    public int getItemCount() {
        return null!=newsData?newsData.size():0;
    }
    class myHolder extends RecyclerView.ViewHolder{
        ImageView newsImage;
        TextView newTittle;
        TextView newsTime;

        public myHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            newsImage=itemView.findViewById(R.id.item_image);
            newTittle=itemView.findViewById(R.id.text_title);
            newsTime=itemView.findViewById(R.id.text_time);
        }
    }
}
